#include "sprite.h"

#include <SDL/SDL.h>
#include <map>
#include <iostream>
#include <vector>

using namespace std;

/******************************************************************************
    Este namespace serve para gerenciar as sprites usadas no jogo
******************************************************************************/
namespace Sprite {

    //Mapa das Sprites
	map<string, Imagem*> m_Sprites;
	typedef map<string, Imagem*>::iterator SpritesIterator;
	
/******************************************************************************
    Carregar(chave, arquivo, tamanho, frames)
    Instancia uma nova sprite na memoria
******************************************************************************/
	void Carregar(std::string key, std::string arquivo, int w, int h, std::vector<SDL_Rect> frames) {
        Imagem* img = new Imagem(arquivo, w, h, frames);
        m_Sprites[key] = img;
	}

/******************************************************************************
    Get(chave)
    Recupera uma imagem instanciada
******************************************************************************/
	Imagem* Get(std::string chave) {
		SpritesIterator pos = m_Sprites.find(chave);
		if (pos == m_Sprites.end()) return NULL;
		return pos->second;
	}

/******************************************************************************
    __Descarregar(sprite)
    Destroi uma sprite
******************************************************************************/
	void __Descarregar(Sprite::Imagem *sprite) {
		if (sprite != NULL){
            delete(sprite);
		}
	}

/******************************************************************************
    Descarregar(chave)
    Desroi uma sprite e remove ela do mapa
******************************************************************************/
	void Descarregar(std::string chave) {
		__Descarregar(Get(chave));
		SpritesIterator pos = m_Sprites.find(chave);
		if (pos == m_Sprites.end()) return;
		m_Sprites.erase(pos);
	}

}
